<template>
  <div class="order-list">
    <div class="page-header">
      <h2>订单管理</h2>
      <div class="stats">
        <div class="stat-item">待处理: {{ pendingOrders.length }}</div>
        <div class="stat-item">已完成: {{ completedOrders.length }}</div>
      </div>
    </div>

    <div v-if="loading" class="loading">加载中...</div>
    <div v-else-if="error" class="error">错误: {{ error }}</div>
    <div v-else class="orders-container">
      <!-- 待处理订单 -->
      <div class="orders-section">
        <h3>待处理订单</h3>
        <div v-if="pendingOrders.length === 0" class="no-orders">暂无待处理订单</div>
        <div v-else class="orders-list">
          <div v-for="order in pendingOrders" :key="order.id" class="order-card pending">
            <div class="order-header">
              <span class="order-number">订单号: {{ order.orderNumber }}</span>
              <span class="order-date">{{ formatDate(order.createdAt) }}</span>
            </div>
            <div class="order-items">
              <div v-for="item in order.items" :key="item.productId" class="order-item">
                <span class="item-name">{{ getProductName(item.productId) }}</span>
                <span class="item-quantity">×{{ item.quantity }}</span>
                <span class="item-price">¥{{ (item.price * item.quantity).toFixed(2) }}</span>
              </div>
            </div>
            <div class="order-footer">
              <span class="total-price">总计: ¥{{ (order.total || 0).toFixed(2) }}</span>
              <button @click="completeOrder(order.id)" class="complete-btn">完成订单</button>
            </div>
          </div>
        </div>
      </div>

      <!-- 已完成订单 -->
      <div class="orders-section">
        <h3>已完成订单</h3>
        <div v-if="completedOrders.length === 0" class="no-orders">暂无已完成订单</div>
        <div v-else class="orders-list">
          <div v-for="order in completedOrders" :key="order.id" class="order-card completed">
            <div class="order-header">
              <span class="order-number">订单号: {{ order.orderNumber }}</span>
              <span class="order-date">{{ formatDate(order.createdAt) }}</span>
            </div>
            <div class="order-items">
              <div v-for="item in order.items" :key="item.productId" class="order-item">
                <span class="item-name">{{ getProductName(item.productId) }}</span>
                <span class="item-quantity">×{{ item.quantity }}</span>
                <span class="item-price">¥{{ (item.price * item.quantity).toFixed(2) }}</span>
              </div>
            </div>
            <div class="order-footer">
              <span class="total-price">总计: ¥{{ (order.total || 0).toFixed(2) }}</span>
              <span class="status completed">已完成</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, computed } from 'vue'
import { storeToRefs } from 'pinia'
import { useOrderStore } from '../../stores/order'
import { useProductStore } from '../../stores/product'

const orderStore = useOrderStore()
const productStore = useProductStore()
const { orders, loading, error } = storeToRefs(orderStore)
const { products } = storeToRefs(productStore)

const pendingOrders = computed(() => {
  return orders.value.filter(order => order.status === 'pending')
})

const completedOrders = computed(() => {
  return orders.value.filter(order => order.status === 'completed')
})

// 根据商品ID获取商品名称
const getProductName = (productId) => {
  const product = productStore.getProductById(productId)
  return product ? product.name : '未知商品'
}

onMounted(async () => {
  // 同时加载订单和商品数据
  await Promise.all([
    orderStore.fetchOrders(),
    productStore.fetchProducts()
  ])
})

const completeOrder = async (orderId) => {
  if (confirm('确定要完成这个订单吗？')) {
    try {
      await orderStore.updateOrderStatus(orderId, 'completed')
      alert('订单已完成！')
    } catch (err) {
      alert('完成订单失败：' + err.message)
    }
  }
}

const formatDate = (dateString) => {
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}
</script>

<style scoped>
.order-list {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h2 {
  margin: 0;
  color: #333;
}

.stats {
  display: flex;
  gap: 20px;
}

.stat-item {
  background: #f8f9ff;
  color: #667eea;
  padding: 8px 12px;
  border-radius: 4px;
  font-weight: 500;
}

.loading, .error, .empty {
  text-align: center;
  padding: 40px;
  font-size: 16px;
}

.error {
  color: #e74c3c;
}

.empty {
  color: #666;
}

.orders-container {
  display: flex;
  flex-direction: column;
  gap: 30px;
}

.orders-section h3 {
  color: #333;
  margin-bottom: 15px;
  font-size: 18px;
}

.no-orders {
  color: #666;
  font-style: italic;
  padding: 20px;
  text-align: center;
  background: #f8f9fa;
  border-radius: 5px;
}

.orders-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.order-card {
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 20px;
  background: white;
  transition: box-shadow 0.2s;
}

.order-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.order-card.pending {
  border-left: 4px solid #ffa502;
}

.order-card.completed {
  border-left: 4px solid #2ed573;
}

.order-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #f0f0f0;
}

.order-number {
  font-weight: bold;
  color: #333;
}

.order-date {
  color: #666;
  font-size: 14px;
}

.order-items {
  margin-bottom: 15px;
}

.order-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid #f8f8f8;
}

.order-item:last-child {
  border-bottom: none;
}

.item-name {
  flex: 1;
  color: #333;
}

.item-quantity {
  color: #666;
  margin: 0 10px;
}

.item-price {
  color: #e74c3c;
  font-weight: 500;
}

.order-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 10px;
  border-top: 1px solid #f0f0f0;
}

.total-price {
  font-size: 16px;
  font-weight: bold;
  color: #e74c3c;
}

.status {
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

.status.completed {
  background: #2ed573;
  color: white;
}

.complete-btn {
  background: #667eea;
  color: white;
  border: none;
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background 0.2s;
}

.complete-btn:hover {
  background: #5a67d8;
}
</style>